package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Flink07_State_keyed_Aggregate {

    public static class Avg{
        public int sum = 0;
        public long count = 0L;

        public Double avg() { return sum * 1.0 / count; }
    }

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment
                .getExecutionEnvironment()
                .setParallelism(3);
        env
                .socketTextStream("hadoop162",9999)
                .map(value -> {
                    String[] datas = value.split(",");
                    return new WaterSensor(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2]));

                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    private AggregatingState<WaterSensor, String> vcAvgState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        vcAvgState = getRuntimeContext()
                                .getAggregatingState(
                                        new AggregatingStateDescriptor<WaterSensor, Avg, String>(
                                                "vcAvgState",
                                                new AggregateFunction<WaterSensor, Avg, String>() {
                                                    @Override
                                                    public Avg createAccumulator() {
                                                        return new Avg();
                                                    }

                                                    @Override
                                                    public Avg add(WaterSensor value, Avg acc) {
                                                        acc.sum += value.getVc();
                                                        acc.count++;
                                                        return acc;
                                                    }

                                                    @Override
                                                    public String getResult(Avg acc) {
                                                        return acc.avg() + "";
                                                    }

                                                    @Override
                                                    public Avg merge(Avg a, Avg b) {
                                                        return null;
                                                    }
                                                },
                                                Avg.class
                                        )
                                );
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                        vcAvgState.add(value);

                        out.collect(ctx.getCurrentKey() + " 平均水位: " + vcAvgState.get());
                    }
                })
                .print();

        env.execute();
    }
}
